﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChurchData.Business;

namespace ChurchData
{
    public partial class PeopleContactUC : UserControl
    {
        #region Fields / Properties
        private int PeoplePK { get; set; }
        private PeopleContact drPC { get; set; }
        private bool hasUnsavedChanges { get; set; }

        public bool HasUnsavedChanges
        {
            get
            {
                if (peopleAddressUC1.HasUnsavedChanges || peopleAddressUC2.HasUnsavedChanges)
                    return true;
                else
                    return hasUnsavedChanges;
            }
            set
            {
                hasUnsavedChanges = value;
            }
        }

        #endregion Fields / Properties

        #region Constructor & Form Load
        public PeopleContactUC()
        {
            InitializeComponent();
        }

        private void PeopleContactUC_Load(object sender, EventArgs e)
        {
            this.SetChangeEvents();
        }

        #endregion Constructor & Form Load

        #region Populate Methods
        public void LoadPersonInfo(int peoplePK)
        {
            this.PeoplePK = peoplePK;

            PeopleContactBO pcBO = new PeopleContactBO();
            drPC = pcBO.GetRecordByPeoplePK(PeoplePK);
            if (drPC == null) drPC = new PeopleContact();

            txtHomePhone.Text = drPC.HomePhone;
            txtCellPhone.Text = drPC.CellPhone;
            txtWorkPhone.Text = drPC.WorkPhone;
            txtPersonalEmail.Text = drPC.PersonalEmail;
            txtFamilyEmail.Text = drPC.FamilyEmail;
            txtPreferredContactMethod.Text = drPC.PreferredContactMethod;
            ckxEBulletin.Checked = (drPC.EBulletinInd != null) ? Convert.ToBoolean(drPC.EBulletinInd) : false;

            if (drPC.PrimaryAddressFK != null) this.peopleAddressUC1.LoadAddress(Convert.ToInt32(drPC.PrimaryAddressFK));
            if (drPC.AlternativeAddressFK != null) this.peopleAddressUC2.LoadAddress(Convert.ToInt32(drPC.AlternativeAddressFK));

            if (UserInformation.ViewOnlyInd) this.SetReadOnly();
        }

        private void SetReadOnly()
        {
            txtHomePhone.Enabled = false;
            txtCellPhone.Enabled = false;
            txtWorkPhone.Enabled = false;
            txtPersonalEmail.Enabled = false;
            txtFamilyEmail.Enabled = false;
            txtPreferredContactMethod.Enabled = false;
            peopleAddressUC1.Enabled = false;
            peopleAddressUC2.Enabled = false;
        }
        #endregion Populate Methods

        #region Validate and Save Methods
        public void SaveIt()
        {
            drPC.PeopleFK = PeoplePK;
            drPC.HomePhone = txtHomePhone.Text.Trim();
            drPC.CellPhone = txtCellPhone.Text.Trim();
            drPC.WorkPhone = txtWorkPhone.Text.Trim();
            drPC.PersonalEmail = txtPersonalEmail.Text.Trim();
            drPC.FamilyEmail = txtFamilyEmail.Text.Trim();
            drPC.PreferredContactMethod = txtPreferredContactMethod.Text.Trim();
            drPC.ActiveInd = true;

            if (drPC.EBulletinInd == true && !ckxEBulletin.Checked)
                drPC.EBulletinUnsubscribeDate = DateTime.Today;
            if (drPC.EBulletinInd == false || drPC.EBulletinInd == null && ckxEBulletin.Checked)
                drPC.EBulletinSignupDate = DateTime.Today;
            drPC.EBulletinInd = ckxEBulletin.Checked;

            drPC.LastUpdTimestamp = DateTime.Now;
            drPC.LastUpdUser = UserInformation.UserName;

            if (drPC.PrimaryAddressFK == null)
                drPC.PrimaryAddressFK = this.peopleAddressUC1.SaveAddress(0);
            else
                this.peopleAddressUC1.SaveAddress(Convert.ToInt32(drPC.PrimaryAddressFK));

            if (drPC.AlternativeAddressFK == null)
                drPC.AlternativeAddressFK = this.peopleAddressUC2.SaveAddress(0);
            else
                this.peopleAddressUC2.SaveAddress(Convert.ToInt32(drPC.AlternativeAddressFK));

            if (drPC.PeopleContactPK == 0)
                drPC = new PeopleContactBO().AddRecord(drPC);
            else
                drPC = new PeopleContactBO().UpdateRecord(drPC);

            hasUnsavedChanges = false;
        }

        #endregion Validate and Save Methods

        #region Control Methods
        private void SetChangeEvents()
        {
            txtHomePhone.TextChanged += new EventHandler(controlChanged);
            txtCellPhone.TextChanged += new EventHandler(controlChanged);
            txtWorkPhone.TextChanged += new EventHandler(controlChanged);
            txtPersonalEmail.TextChanged += new EventHandler(controlChanged);
            txtFamilyEmail.TextChanged += new EventHandler(controlChanged);
            txtPreferredContactMethod.TextChanged += new EventHandler(controlChanged);
            ckxEBulletin.CheckedChanged += new EventHandler(controlChanged);
        }

        private void controlChanged(object sender, EventArgs e)
        {
            this.HasUnsavedChanges = true;
        }

        private void btnUpdateAddresses_Click(object sender, EventArgs e)
        {
            // Find the head of household record
            PeopleBO pBO = new PeopleBO();
            People headOfHouse = pBO.GetHeadOfHouseByPeoplePK(this.PeoplePK);

            PeopleContactBO pcBO = new PeopleContactBO();
            PeopleContact headOfHouseContact = pcBO.GetRecordByPeoplePK(headOfHouse.PeoplePK);

            if (headOfHouseContact != null)
            {
                if (headOfHouseContact.PrimaryAddressFK != null)
                {
                    this.peopleAddressUC1.LoadAddress(Convert.ToInt32(headOfHouseContact.PrimaryAddressFK));
                    this.peopleAddressUC1.HasUnsavedChanges = true;
                }

                if (headOfHouseContact.AlternativeAddressFK != null)
                {
                    this.peopleAddressUC2.LoadAddress(Convert.ToInt32(headOfHouseContact.AlternativeAddressFK));
                    this.peopleAddressUC2.HasUnsavedChanges = true;
                }
            }
        }
        #endregion Control Methods
    }
}